<?php

	session_start();
	require_once('../config.php');
	include_once("../database/DataBase.php");
	include_once("function.php");

	$cid = $_POST["cid"];
	$promo_code = $_POST["promo_code"];
	$uid = $_POST["uid"];
	$total = $_POST["total"];
	
	$product_id = $_POST["product_id"];
	$product_price = $_POST["product_price"];
	$product_amount = $_POST["product_amount"];
	
	$database = New DataBase();
	$database->open_connection();
		
	if($promo_code != ""){
		$discountSet = $database->getDiscountForCalculate($promo_code, $cid);
		$discount = mysql_fetch_assoc($discountSet);
		
		$type = $discount["type"];
		$discountData = $discount["discount"];
	}
	
	$tempString = "";
	for($i=0;$i<count(price_list);$i++){
		$product_list = array($product_id, $product_amount, $product_price);
	}
	
	$total = calculate_total($type, $discountData, $product_list);
	$result = $database->addInvoice($cid, $promo_code, $total, $uid, $product_list);
	
	if($result){
		$database->LogAction($_SESSION["login_id"], "Add", "Add new areas from ".getIpAddress());
		$_SESSION["message"] = "New Invoice has been created";
		header('Location: ../Message.php');
	}
	else{
		echo "fail";
	}
	
	$database->close_connection();
?>
